#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
import socket
from gevent import spawn
from threading import Thread


def talk(connect):
    print(connect)
    while True:
        try:
            data = connect.recv(1024)
            # print(data)
            if len(data) == 0: break
            connect.send(data.upper())
        except ConnectionResetError:
            break
    # conn.close()


def server(ip, port):
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind((ip, port))
    server.listen(10)

    print("start...")
    while True:
        conn, addr = server.accept()
        x = spawn(talk, conn)
        x.join()


if __name__ == '__main__':
    # t = Thread(target=server, args=("127.0.0.1", 2299))
    # t.start()
    g = spawn(server, "127.0.0.1", 8080)
    g.join()
